using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace HtmlReport
{
    public partial class HtmlReportViewer : UserControl
    {
        public HtmlReportViewer()
        {
            InitializeComponent();

        }
        public static string TmpDir
        {
            set
            {
                HtmlReportExporter.TmpDir = value;
            }
            get
            {
                return HtmlReportExporter.TmpDir;
            }
        }
        public event CancelEventHandler OnDocumentPrinting;
        public bool ShowToolBar
        {
            get{
                return this.toolStrip1.Visible;
            }
            set{
                this.toolStrip1.Visible=value;
                this.document.BringToFront();
                this.Refresh();
            }

        }
        public DataSet DataSource
        {
            set
            {
                document.DataSource = value;
            }
            get
            {
                return document.DataSource;
            }
        }
        public void LoadReport(string FileName)
        {
            document.Load(FileName);
        }
        public void ExportToWord(string FileName, bool OpenFileInNewWindow)
        {
            document.ExportToWord( FileName,  OpenFileInNewWindow);
        }
        public void ShowReport()
        {
            document.ShowReport();
            lblPageCount.Text = "1/"+document.PageCount.ToString();
        }
        public HtmlReportDocument ReportDocument
        {
            get
            {
                return document;
            }
        }
        private void UpdatePageIndex()
        {
            txtPageIndex.Text = Convert.ToString(document.PageIndex+1);
        }
        private void btnPrintPreview_Click(object sender, EventArgs e)
        {
            this.document.ShowPageSetupDialog();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (this.OnDocumentPrinting != null)
            {
                CancelEventArgs args = new CancelEventArgs();
                args.Cancel = false;
                this.OnDocumentPrinting(this, args);
                if (args.Cancel) return;
            }
            this.document.PrintReport();
    
        }

        private void btnMoveFirst_Click(object sender, EventArgs e)
        {
            this.document.MoveFirst();
        }

        private void btnMoveLast_Click(object sender, EventArgs e)
        {
            this.document.MoveLast();
        }

        private void btnMovePrev_Click(object sender, EventArgs e)
        {
            this.document.MovePrevious();
        }

        private void btnMoveNext_Click(object sender, EventArgs e)
        {
            this.document.MoveNext();
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            int PageIndex = 0;
            int.TryParse(txtPageIndex.Text,out PageIndex);
            document.ShowReport(PageIndex);
        }

        private void txtPageIndex_Validating(object sender, CancelEventArgs e)
        {
            int pageIndex = 0;
            int.TryParse(txtPageIndex.Text.Trim(),out pageIndex);
            if (pageIndex > document.PageCount)
                pageIndex = document.PageCount;
            if (pageIndex <= 0) pageIndex = 0;
            txtPageIndex.Text = pageIndex.ToString();
        }

        private void document_OnPageIndexChanged(object sender, EventArgs e)
        {
            UpdatePageIndex();
        }

        private void btnExportToWord_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "Microsoft Word Document(*.doc)|*.doc|All Files(*.*)|*.*";
            if (save.ShowDialog() == DialogResult.OK)
            {
                this.document.ExportToWord(save.FileName, true);
            }
        }

        private void btnExportToExcel_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "Microsoft Excel Document(*.xls)|*.xls|All Files(*.*)|*.*";
            if (save.ShowDialog() == DialogResult.OK)
            {
                this.document.ExportToWord(save.FileName, true);
            }
        }

        private void HtmlReportViewer_Load(object sender, EventArgs e)
        {
            this.document.OnDocumentShowed += new EventHandler(document_OnDocumentShowed);
        }

        void document_OnDocumentShowed(object sender, EventArgs e)
        {
            this.lblPageCount.Text = "/" + document.PageCount.ToString();
        }

        private void mnuViewSource_Click(object sender, EventArgs e)
        {
            FrmViewSource frm = new FrmViewSource();
            frm.Source = document.ReportContent;
            frm.ShowDialog();
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            this.document.PrintPreview();
        }
    }
}
